# src/api/openai_api.py
import openai
import time

class OpenAIClient:
    def __init__(self, api_key, base_url):
        self._client = openai.OpenAI(api_key=api_key, base_url=base_url)
    
    @property
    def client(self):
        """获取底层的 OpenAI 客户端"""
        return self._client

    def get_answer(self, history_paper, model="moonshot-v1-8k", temperature=0.3):
        try:
            completion = self._client.chat.completions.create(
                model=model,
                messages=history_paper,
                temperature=temperature,
            )
            return completion.choices[0].message.content
        except openai.RateLimitError as e:
            print(f"Rate limit exceeded. Retrying in {e.retry_after} seconds.")
            time.sleep(e.retry_after)
            return self.get_answer(history_paper, model, temperature)